package com.jiajia.server.controller;

import com.jiajia.server.pojo.Admin;
import com.jiajia.server.pojo.AdminLoginParm;
import com.jiajia.server.pojo.RespBean;
import com.jiajia.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author ZSJia
 * @date 2021/8/10 010 - 15:47
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {

        @Autowired
        private IAdminService iAdminService;
    /**
     * 登陆成功返回token
     * @param adminLoginParm
     * @param request
     * @return
     */
    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean loginToken(@RequestBody AdminLoginParm adminLoginParm, HttpServletRequest request){
       return     iAdminService.login(adminLoginParm,request);
    }


    @ApiOperation(value = "获取登录的用户信息")
    @GetMapping("/admin/info")
    public  Admin getAdminInfo(Principal principal){
        if (principal == null){
            return  null;
        }
        String username = principal.getName();
        Admin admin =  iAdminService.getAdminByUserName(username);
        admin.setPassword(null);
        admin.setRoles(iAdminService.getRole(admin.getId()));
        return admin;
    }


    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录")
     @GetMapping("/logout")
    public  RespBean logout(){
        return RespBean.success("退出登录");
    }
}
